package com.yaozheng.vocationaltraining.adapter;

import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.androidannotations.annotations.rest.Post;

import com.bumptech.glide.Glide;
import com.yaozheng.vocationaltraining.activity.BaseActivity;
import com.yaozheng.vocationaltraining.utils.BitmapLoader;
import com.yaozheng.vocationaltraining.utils.TypeUtils;
import com.yaozheng.vocationaltraining.view.LearnListContentItemView;
import com.yaozheng.vocationaltraining.view.LearnListContentItemView_;

import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

/**
 * 学习列表内容设备器
 *
 * @author Administrator
 */
public class LearnListContentAdapter extends BaseAdapter {
    BaseActivity baseActivity;
//	private List<JSONObject> dataList;

    private JSONArray dataList;

    BitmapLoader bitmapLoader;

    public LearnListContentAdapter(BaseActivity baseActivity) {
        super();
        this.baseActivity = baseActivity;
        bitmapLoader = new BitmapLoader(Glide.with(baseActivity));
    }


    /**
     * 添加
     *
     * @param dataList
     */
    public void setDataList(JSONArray dataList) {
//        if (this.dataList == null) {
//            this.dataList = new JSONArray();
//        } else {
//            this.dataList.clear();
//        }
//        this.dataList.addAll(dataList);
        this.dataList = dataList;
    }

    @Override
    public int getCount() {
        if (dataList == null)
            return 0;
        return TypeUtils.getJsonArraySize(dataList);
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LearnListContentItemView learnListContentItemView;

        if (convertView == null) {
            learnListContentItemView = LearnListContentItemView_
                    .build(baseActivity);
        } else {
            learnListContentItemView = (LearnListContentItemView) convertView;
        }
        learnListContentItemView.loadData(position, TypeUtils.getJsonObject(dataList, position), bitmapLoader);
        return learnListContentItemView;
    }

}
